home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.07 Jul 96 / 01 - popup Applet / 2nd version / popup.java < prev   
Encoding:
Java Source  |  1996-04-05  |  1.9 KB  |  87 lines  |  [TEXT/CWIE]

  1. /* Copyright 1996 by Dave Mark, All Rights Reserved */
  2.  
  3. import java.applet.*;
  4. import java.awt.*;
  5. import java.net.*;
  6.  
  7. public class popup extends Applet
  8. {
  9.     private Choice    urlChoices;
  10.     
  11.     public void    init()
  12.     {
  13.         int        numURLs = 0;
  14.         
  15.         this.setBackground( Color.yellow );//java.awt.Color
  16.         this.setForeground( Color.red );//java.awt.Color
  17.         
  18.         urlChoices = new Choice();
  19.             
  20.         urlChoices.setForeground( Color.green );
  21.         urlChoices.setBackground( Color.blue );
  22.         
  23.         String numURLsString = this.getParameter( "numURLs" );
  24.         
  25.         if ( numURLsString == null )
  26.         {
  27.             this.showStatus( "null or missing numURLs Parameter!" );
  28.         }
  29.         else
  30.         {
  31.             try
  32.             {
  33.                 numURLs = Integer.parseInt( numURLsString );
  34.             }
  35.             catch( NumberFormatException err )
  36.             {
  37.                 numURLs = 0;
  38.                 this.showStatus( "Bad numURLs Parameter: " + numURLsString );
  39.             }
  40.         
  41.             String        urlString;
  42.             
  43.             for ( int i=1; i<=numURLs; i++ )
  44.             {
  45.                 urlString = this.getParameter( Integer.toString( i ) );
  46.                 
  47.                 if ( urlString == null )
  48.                     this.showStatus( "Missing Parameter: " + i );
  49.                 else
  50.                     urlChoices.addItem( urlString );
  51.             }
  52.         }
  53.         
  54.         this.add( new Label( "Select URL: " ) );
  55.         this.add( urlChoices );
  56.     }
  57.     
  58.     public boolean action( Event e, Object obj )
  59.     {
  60.         URL        url;
  61.         
  62.         if ( e.target == urlChoices )
  63.         {
  64.             try
  65.             {
  66.                 url = new URL( obj.toString() );
  67.             }
  68.             catch( MalformedURLException err )
  69.             {
  70.                 // Could print error message here.
  71.                 return true;
  72.             }
  73.             /* If a method throws an exception, you must be prepared to catch it,
  74.                 even if you ignore the result. See java.net.URL().
  75.                 If I throw an exception, I'll create a new java.lang.exception
  76.                 object (or some class that extends throwable). But if I catch an
  77.                 exception, the exception object will be passed to me, just like err was here.
  78.                 Take a look at Throwable to see what you can do with err (especially getMessage()).
  79.                 
  80.             */
  81.             this.getAppletContext().showDocument( url );
  82.             return true;
  83.         }
  84.         else
  85.             return super.action( e, obj );
  86.     }
  87. }